#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

cmd=$( basename "$0" )

function usage() {
	echo "Usage: ${cmd} [ -t | -c | -h | -n | -p ]"
    echo -e "\t-c: 增加对cuda代码的索引"
    echo -e "\t-t: 增加对test代码的索引"
    echo -e "\t-p: 增加对python代码的索引"
    echo -e "\t-n: dry-run"
    echo -e "\t-h: 显示帮助信息"
	exit 1
}

function build_file_list() {
    # 重置文件列表
    echo > "${csFile}"
    for d in "${hppDirs[@]}"
    do
        find "$d" ${pruneOpt} ${findTestOpt} -name "*.hpp" -type f -print >> "${csFile}"
        find "$d" ${pruneOpt} ${findTestOpt} -name "*.h" -type f -print >> "${csFile}"
    done
    
    for d in "${cppDirs[@]}"
    do
        find "$d" ${pruneOpt} ${findTestOpt} -name "*.cpp" -type f -print >> "${csFile}"
        find "$d" ${pruneOpt} ${findTestOpt} -name "*.c" -type f -print >> "${csFile}"
        find "$d" ${pruneOpt} ${findTestOpt} -name "*.cc" -type f -print >> "${csFile}"
    done
    
    if ${indexCuda}
    then
        for d in "${cuDirs[@]}"
        do
            find "$d" ${pruneOpt} ${findTestOpt} -name "*.cu" -type f -print >> "${csFile}"
        done
    fi
    
    # 包括proto文件
    for d in "${protoDirs[@]}"
    do
        find "$d" ${pruneOpt} ${findTestOpt} -name "*.proto" -type f -print >> "${csFile}"
    done

    if ${indexPython}
    then
        for d in "${protoDirs[@]}"
        do
            find "$d" ${pruneOpt} ${findTestOpt} -name "*.py" -type f -print >> "${csFile}"
        done
    fi
}

function echo_build_cmd() {
    for d in "${hppDirs[@]}"
    do
        echo find "$d" ${pruneOpt} ${findTestOpt} -name "*.hpp" -type f -print 
        echo find "$d" ${pruneOpt} ${findTestOpt} -name "*.h" -type f -print 
    done
    
    for d in "${cppDirs[@]}"
    do
        echo find "$d" ${pruneOpt} ${findTestOpt} -name "*.cpp" -type f -print 
        echo find "$d" ${pruneOpt} ${findTestOpt} -name "*.c" -type f -print 
        echo find "$d" ${pruneOpt} ${findTestOpt} -name "*.cc" -type f -print 
    done
    
    if ${indexCuda}
    then
        for d in "${cuDirs[@]}"
        do
            echo find "$d" ${pruneOpt} ${findTestOpt} -name "*.cu" -type f -print 
        done
    fi
    
    # 包括proto文件
    for d in "${protoDirs[@]}"
    do
        echo find "$d" ${pruneOpt} ${findTestOpt} -name "*.proto" -type f -print 
    done

    if ${indexPython}
    then
        for d in "${protoDirs[@]}"
        do
            echo find "$d" ${pruneOpt} ${findTestOpt} -name "*.python" -type f -print 
        done
    fi
}

protoDirs=(onnxruntime)
# 包括.c,.cc文件所在目录
cppDirs=(onnxruntime)
cuDirs=(onnxruntime)
# 包含.h文件所在目录
hppDirs=(onnxruntime include)

# 去掉不需要的EP代码
pruneOpt="-wholename */providers/acl -type d -prune -o "
pruneOpt="-wholename */providers/armnn -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/azure -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/cann -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/coreml -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/dml -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/dnnl -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/migraphx -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/openvino -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/qnn -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/rknpu -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/rocm -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/snpe -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/tensorrt -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/tvm -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/vitisai -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/winml -type d -prune -o ${pruneOpt}"
pruneOpt="-wholename */providers/xnnpack -type d -prune -o ${pruneOpt}"
# 默认不索引test目录下的文件
findTestOpt="-name test -type d -prune -o"
indexCuda=false
dryRun=false
indexPython=false
while getopts "ctpnh" args
do
    case "${args}" in 
        c)
            indexCuda=true
            ;;
        t)
            findTestOpt=""
            ;;
        p)
            indexPython=true
            ;;
        n)
            dryRun=true
            ;;
        *)
            usage;;
    esac
done

if ${dryRun}
then
    echo_build_cmd
else
    csFile="cscope.files"
    echo "Build ${csFile} ..."
    build_file_list
    # 建立cscope数据库
    echo "Build cscope DB..."
    cscope -Rbqk
    echo "Done."
fi
